var person = {
    //value: 10
    //writable: true
    //enumerable true
    //configurable: ture
    age: 10 
};

Object.defineProperty(person, "prox", {
    //value: undefined
    //writable: false
    //enumerable: false
    //configurable: true
}); 

Object.defineProperty(person, "name", {
    value: "Lys"
});

Object.defineProperty(person, "name", {
    configurable: false //后续无法修改为true
}

//对象访问器属性get/set
var person = {
    language: "en",
    get lang() { 
        return this.language;
    },
    set lang(lang) {
        this.language = lang;
    }
};
console.log(person.lang);
person.lang = "zh";
console.log(person.lang);
// 定义对象
var obj = {counter : 0};

// 定义 setters
Object.defineProperty(obj, "reset", {
  get : function () {this.counter = 0;}
});
Object.defineProperty(obj, "increment", {
  get : function () {this.counter++;}
});
Object.defineProperty(obj, "decrement", {
  get : function () {this.counter--;}
});
Object.defineProperty(obj, "add", {
  set : function (value) {this.counter += value;}
});
Object.defineProperty(obj, "subtract", {
  set : function (value) {this.counter -= value;}
});

// 操作计数器：
obj.reset; //counter: 0
obj.add = 5; //counter: 5
obj.subtract = 1; //4
obj.increment; //5
obj.decrement; //4
obj.counter; //4

//混合定义
var book = {};
Object.defineProperties(book, {
    _year: {
        value: 2004
    },
    edition: {
        value: 1
    },
    year: {
        get: function(){
            return this._year;
        },
        set: function(newValue){
            if (newValue > 2004) {
                this._year = newValue;
                this.edition += newValue - 2004;
            }
        }
    }
});
//获取属性特性
var descriptor = Object.getOwnPropertyDescriptor(book, "_year");
console.log(descriptor.value); //2004
console.log(descriptor.configurable); //false
console.log(typeof descriptor.get); //"undefined"
descriptor = Object.getOwnPropertyDescriptor(book, "year");
console.log(descriptor.value); //undefined
console.log(descriptor.enumerable); //false
console.log(typeof descriptor.get); //"function"
